package com.yifeixi.validation.entity;

import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yifeixi.validation.group.IdGroup;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import javax.validation.Valid;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Email;
import javax.validation.constraints.FutureOrPresent;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Pattern.Flag;
import javax.validation.constraints.Positive;
import javax.validation.constraints.Size;
import javax.validation.groups.ConvertGroup;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 用户<br> 验证参数都设置符合条件的默认值
 *
 * @author YiFeiXi
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class User {

  @NotNull(groups = IdGroup.class)
  private Integer id = 1;

  @NotBlank
  private String name = "张三";

  @Min(1)
  @Max(200)
  private Integer age = 20;

  @Past
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private LocalDateTime birthday = LocalDateTime.now().minusDays(1);

  @FutureOrPresent
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private LocalDateTime nextBirthday = LocalDateTime.now().plusDays(1);

  @Digits(integer = 4, fraction = 2)
  @DecimalMax(value = "1000")
  @DecimalMin(value = "0")
  private BigDecimal money = new BigDecimal(10);

  private List<@Email String> email = CollUtil.newArrayList("123456@qq.com");

  @NotBlank
  private String username = "username@qq.com";

  @Size(max = 1)
  private List<String> nickname = CollUtil.newArrayList("小三子");

  @Positive
  private Double height = 175D;

  @Pattern(
      regexp =
          "(http|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?",
      flags = Flag.CASE_INSENSITIVE)
  private String blog = "http://myblogs.work";

  /**
   * role 作为关联对象<br> 只需要 role.id 值，因此只校验role.id
   */
  @NotEmpty
  @Valid
  @ConvertGroup(to = IdGroup.class)
  private List<Role> roles = CollUtil.newArrayList(new Role());
}
